package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import DAO.BaseDAO;
import DTO.Employee;
import serviceImpl.VipDAO;
import pageUtil.PageB;
import service.PageService;
import util.UtilMD5;
/**
 *
 * @author Administrator
 *
 */
@WebServlet(name = "all_emp",urlPatterns = "/all_emp")
public class VipServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public VipServlet() {
    }
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag = null;
        flag = request.getParameter("flag");
        switch(flag){
            case "findall":
                try {
                    find_all(request,response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case "findwill":
                try {
                    find_will(request,response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case "findout":
                try {
                    find_out(request,response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case "redact":
                redact(request,response);
                break;

            case "redact1":
                redact1(request,response);
                break;

            case "check":
                check(request,response);
                break;

            case "checkok":
                checkok(request,response);
                break;

            case "update_a":
                try {
                    update_a(request,response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case "change_cond":
                change_cond(request,response);
                break;

            case "delete":
                delete(request,response);
                break;

            case "searchall":
                searchall(request,response);
                break;

            case "update_psw":
                update_psw(request,response);
                break;

            default:
                break;
        }



    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }



    protected static void update_psw(HttpServletRequest request, HttpServletResponse response){
        try {
            String old_psw = request.getParameter("old_psw");
            String new_psw = request.getParameter("new_psw1");
            String id = request.getParameter("id");
            System.out.println(old_psw);
            System.out.println(new_psw);
            System.out.println(id);
            BaseDAO<Employee> ed = new VipDAO();
            Employee emp = new Employee();
            emp = ed.findById(id);
            if(UtilMD5.MD5U(old_psw).equals(emp.getPassword())){
                VipDAO empdao = new VipDAO();
                emp.setId(id);
                emp.setPassword(UtilMD5.MD5U(new_psw));
                boolean boo = empdao.update_psw(emp);
                if(boo){

                    Cookie[] cks = request.getCookies();


                    for(Cookie ck:cks){
                        if("e_username".equals(ck.getName())){
                            ck.setMaxAge(0);
                            response.addCookie(ck);
                        }
                        if("e_password".equals(ck.getName())){
                            ck.setMaxAge(0);
                            response.addCookie(ck);
                        }
                    }
                    request.getSession().invalidate();
                    PrintWriter out = response.getWriter();
                    out.print("<script>top.location.href='login.jsp';</script>");
                    out.flush();
                }else{
                    request.setAttribute("msg", "修改失败");
                    request.getRequestDispatcher("update_emp_psw.jsp").forward(request, response);
                }

            }else{
                request.setAttribute("error", "原密码输入错误");
                request.getRequestDispatcher("update_emp_psw.jsp").forward(request, response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    protected static void searchall(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
        String thing = new String(request.getParameter("search").trim().getBytes("ISO8859-1"),"utf-8");
        String sex = request.getParameter("select_sex");
        String condition = request.getParameter("select_conditionn");
        BaseDAO<Employee> ed = new VipDAO();

        String sql = "SELECT id,eid,name,password,sex,age,"
                + "job,education,regtime,depid,conditionn,"
                + "specialty,active,message,phone "
                + "FROM employee "
                + "WHERE CONCAT(IFNULL(id,''),IFNULL(eid,''),IFNULL(password,''),"
                + "IFNULL(name,''),IFNULL(age,''),IFNULL(password,''),"
                + "IFNULL(sex,''),IFNULL(job,''),IFNULL(education,''),"
                + "IFNULL(regtime,''),IFNULL(depid,''),IFNULL(conditionn,''),"
                + "IFNULL(specialty,''),IFNULL(message,''),IFNULL(phone,'')) "
                + "LIKE '%"+thing+"%' and sex="+sex+" and conditionn="+condition+" ";
        String sql2 = "SELECT count(id) "
                + "FROM employee "
                + "WHERE CONCAT(IFNULL(id,''),IFNULL(eid,''),IFNULL(password,''),"
                + "IFNULL(name,''),IFNULL(age,''),IFNULL(password,''),"
                + "IFNULL(sex,''),IFNULL(job,''),IFNULL(education,''),"
                + "IFNULL(regtime,''),IFNULL(depid,''),IFNULL(conditionn,''),"
                + "IFNULL(specialty,''),IFNULL(message,''),IFNULL(phone,'')) "
                + "LIKE '%"+thing+"%' and sex="+sex+" and conditionn="+condition+" ";

        try {
            List<Employee> list = ed.findAll(sql);
            int count = ed.dataCount(sql2);
            request.setAttribute("list", list);
            request.setAttribute("count", count);
            request.getRequestDispatcher("vip_search.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    protected static void delete(HttpServletRequest request, HttpServletResponse response){
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            String id = request.getParameter("id");
            Employee emp = new Employee();
            BaseDAO<Employee> ud = new VipDAO();
            emp.setId(id);
            if(ud.delete(emp)){
                request.setAttribute("msg","删除成功");
                request.getRequestDispatcher("vip_list.jsp").forward(request, response);
            }else{
                request.setAttribute("msg","删除失败");
                request.getRequestDispatcher("vip_list.jsp").forward(request, response);

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    protected static void change_cond(HttpServletRequest request, HttpServletResponse response) throws ServletException{
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            String id = request.getParameter("id");
            VipDAO ed = new VipDAO();
            Employee emp = new Employee();
            emp.setId(id);
            boolean boo = ed.update_condition(emp);
            if(boo){

                request.setAttribute("msg","修改成功");
                request.getRequestDispatcher("vip_list.jsp").forward(request, response);
            }else{
                request.setAttribute("msg","修改失败");
                request.getRequestDispatcher("vip_list.jsp").forward(request, response);

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    protected static void update_a(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String name = new String(request.getParameter("username").trim().getBytes("ISO8859-1"),"UTF-8");
        int age = Integer.parseInt(request.getParameter("age"));
        String sex = request.getParameter("sex");
        String phone = request.getParameter("phone");
        String job = new String(request.getParameter("job").trim().getBytes("ISO8859-1"),"UTF-8");
        String message =new String(request.getParameter("message").trim().getBytes("ISO8859-1"),"UTF-8");
        String sql = "update employee set name=?,age=?,sex=?,phone=?,message=?,job=? WHERE id = ?";
        BaseDAO<Employee> ed = new VipDAO();
        Employee emp = new Employee();
        emp.setName(name);
        emp.setAge(age);
        emp.setSex(Integer.parseInt(sex));
        emp.setPhone(phone);
        emp.setMessage(message);
        emp.setJob(job);
        emp.setId(id);
        System.out.println(emp.getId() + "*" + emp.getName() + "*" + emp.getAge() + "*" + emp.getSex() + "*" + emp.getMessage() + "*" + emp.getJob());
        boolean boo = ed.update(emp, sql);
        if(boo){
            request.setAttribute("msg","修改成功");
            request.getRequestDispatcher("vip_list.jsp").forward(request, response);
        }else{
            request.setAttribute("msg","修改失败");
            request.getRequestDispatcher("vip_list.jsp").forward(request, response);
        }
    }

    protected static void find_all(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //获取当前页
        String sql = "select id,eid,name,password,sex,age,job,education,"
                + "regtime,depid,conditionn,specialty,active,message,phone from employee where active=1 and conditionn in(1,2) order by regtime desc limit ?,?";
        String sql2 = "select count(id) from employee where active=1 and conditionn in(1,2)";
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String currentPage = request.getParameter("cp");
        currentPage = currentPage == null?"1":currentPage;
        //创建一个分页工具对象
        PageB<Employee> pageBean = new PageB<Employee>();
        pageBean.setCurrentPage(Integer.parseInt(currentPage));
        pageBean.setPageSize(5);
        PageService<Employee> pageservice = new PageService<Employee>(new VipDAO());
        PageB<Employee> bean = pageservice.paging(pageBean,sql,sql2);
        //将java对象转为JSON字符串
        String page = JSON.toJSONString(bean);
        PrintWriter out = response.getWriter();
        out.print(page);
        out.flush();



    }




    protected static void find_will(HttpServletRequest request, HttpServletResponse response) throws Exception{

        String sql = "select id,eid,name,password,sex,age,job,education,"
                + "regtime,depid,conditionn,specialty,active,message,phone from employee where active=0 order by regtime desc limit ?,?";
        String sql2 = "select count(id) from employee where active=0";
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String currentPage = request.getParameter("cp");
        currentPage = currentPage == null?"1":currentPage;
        //创建一个分页工具对象
        PageB<Employee> pageBean = new PageB<Employee>();
        pageBean.setCurrentPage(Integer.parseInt(currentPage));
        pageBean.setPageSize(5);

        PageService<Employee> pageservice = new PageService<Employee>(new VipDAO());
        PageB<Employee> bean = pageservice.paging(pageBean,sql,sql2);
        //将java对象转为JSON字符串
        String page = JSON.toJSONString(bean);
        PrintWriter out = response.getWriter();
        out.print(page);
        out.flush();
    }



    protected static void find_out(HttpServletRequest request, HttpServletResponse response) throws Exception{

        String sql = "select id,eid,name,password,sex,age,job,education,"
                + "regtime,depid,conditionn,specialty,active,message,phone from employee where active=1 and conditionn=3 order by regtime desc limit ?,?";
        String sql2 = "select count(id) from employee where active=1 and conditionn=3";
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String currentPage = request.getParameter("cp");
        currentPage = currentPage == null?"1":currentPage;
        //创建一个分页工具对象
        PageB<Employee> pageBean = new PageB<Employee>();
        pageBean.setCurrentPage(Integer.parseInt(currentPage));
        pageBean.setPageSize(5);

        PageService<Employee> pageservice = new PageService<Employee>(new VipDAO());
        PageB<Employee> bean = pageservice.paging(pageBean,sql,sql2);
        //将java对象转为JSON字符串
        String page = JSON.toJSONString(bean);
        PrintWriter out = response.getWriter();
        out.print(page);
        out.flush();
    }



    protected static void redact(HttpServletRequest request, HttpServletResponse response){
        try {
            response.encodeRedirectURL("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");
            request.setCharacterEncoding("utf-8");
            BaseDAO<Employee> ed = new VipDAO();
            String id = request.getParameter("id");
            Employee emp = new Employee();
            emp = ed.findById(id);
            request.setAttribute("emp", emp);
            request.getRequestDispatcher("redact_vip.jsp").forward(request, response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    protected static void redact1(HttpServletRequest request, HttpServletResponse response){
        try {
            response.encodeRedirectURL("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");
            request.setCharacterEncoding("utf-8");
            BaseDAO<Employee> ed = new VipDAO();
            String id = request.getParameter("id");
            Employee emp = new Employee();
            emp = ed.findById(id);
            request.setAttribute("emp", emp);
            request.getRequestDispatcher("vip_msg.jsp").forward(request, response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    protected static void check(HttpServletRequest request, HttpServletResponse response){
        try {
            response.encodeRedirectURL("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");
            request.setCharacterEncoding("utf-8");
            BaseDAO<Employee> ed = new VipDAO();
            String id = request.getParameter("id");
            Employee emp = new Employee();
            emp = ed.findById(id);
            request.setAttribute("emp", emp);
            request.getRequestDispatcher("redact_vip.jsp").forward(request, response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected static void checkok(HttpServletRequest request, HttpServletResponse response){
        try {
            response.encodeRedirectURL("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");
            request.setCharacterEncoding("utf-8");
            VipDAO ed = new VipDAO();
            String id = request.getParameter("id");
            Employee emp = new Employee();
            emp.setId(id);
            boolean boo = ed.update1(emp);
            if(boo){
                request.setAttribute("msg","审核成功");
                request.getRequestDispatcher("vip_list.jsp").forward(request, response);
            }else{
                request.setAttribute("msg","审核失败");
                request.getRequestDispatcher("vip_list.jsp").forward(request, response);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
